package io.ztd.net.androiduitest;

import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.clearText;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

/**
 * Created by Administrator on 2017/9/20.
 */

@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainTest {
    private String mStringToBetyped;

    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
            MainActivity.class);

    @Before
    public void initValidString() {
        // Specify a valid string.
        mStringToBetyped = "滑动事件";
    }

    @Test
    public void changeText_sameActivity() {
        // Test click
        onView(withId(R.id.tv_click)).perform(click());
        delay();

        // Test text
        onView(withId(R.id.textView))
                .check(matches(withText(mStringToBetyped)));
        delay();

        // 清除文本
        onView(withId(R.id.et_text)).perform(clearText());
        delay();


        // Test closeSoftKeyboard
        onView(withId(R.id.et_text))
                .perform(click(), closeSoftKeyboard());
        delay();
    }

    public void delay() {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
